package team.cdlzz.controller;

import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xalan.internal.xsltc.DOM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.cdlzz.dao.ExamPutMapper;
import team.cdlzz.dao.HomeworkPutMapper;
import team.cdlzz.model.pojo.*;
import team.cdlzz.service.QuestionService;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author zw
 * @date 2022-06-27.
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    QuestionService questionService;
        @Autowired
        ExamPutMapper examPutMapper;
    @RequestMapping("/getStaticCode")
    public String GetStaticCode(){
        return questionService.getStaticCode();
    }


    @RequestMapping("/UpExcel")
    public String setExcelFile(String cid){
        return questionService.setExcelFile(cid);
    }

    @RequestMapping("/getCodeAllList")
    public List<CodeQuestion> getCodeAllList(){
        return  questionService.getCodeDataListAll();
    }

    @RequestMapping("/getCodeQuestion")
    public CodeQuestion getCodeQuestion(Integer qid){
        return questionService.getCodeData(qid);
    }

    /**
     * 提交代码的地方
     * @param codeSubmit
     * @return
     * @throws IOException
     */
    @RequestMapping("/codeCorrect")
    public JSONObject submitCode(@RequestBody CodeSubmit  codeSubmit) throws IOException {
        return questionService.SubmitCode(codeSubmit);
    }

    /**
     * 查重方法
     * @param cid
     * @param uid
     * @return
     */
    @RequestMapping("duplicateChecking")
    public Double DuplicateChecking(@RequestParam("qid") Integer cid, @RequestParam("uid") Integer uid) {
        return questionService.DuplicateChecking(cid, uid);
    }

//        @RequestMapping("duplicateChecking")
//    public String DuplicateChecking(@RequestBody String uid) {
//            System.out.println(uid);
//            return null;
////        return questionService.DuplicateChecking(cid, uid);
//    }
    @RequestMapping("getExamQuestionList")
    public List<Question> getExamQuestionList(@RequestParam("eid") Integer eid){
        return questionService.getExamQuestionList(eid);
    }

    @RequestMapping("/putExam")
    public String  PutWork(@RequestParam("eid") Integer eid,@RequestParam("uid") Integer uid){
//        double so = 0.0;
//        int flag = 0;
//        if (list1!=null&&list2!=null){
//            for (int i = 0; i < list1.length; i++) {
//               flag+= list1[i].equals(list2.get(i))?1:0;
//            }
//        }
//        so = flag/(double)list1.length*100;
//        System.out.println(so);
        Random random = new Random();

        System.out.println(uid+ "===" +eid);
//        int i = random.nextInt();
        ExamPut examPut = new ExamPut();
//        homeworkPut.setUid();
        examPut.setUid(uid);
        examPut.setPutTime(getNowTime());
        examPut.setEid(eid);
        int ends = random.nextInt(2)+3;
        examPut.setScore(ends*10);
        int insert = examPutMapper.insert(examPut);
        if (insert!=0){
            return "考试结束";
        }
        return "服务器内部错误";
    }

//    @RequestMapping("/putExam")
//    public String PutWork(@RequestBody String json){
//        System.out.println(json);
//        return "数据接受正常\n"+json;
//    }

    public String getNowTime(){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }
}